
#include <vector>
#include "poker.h"
#include "assert.h"


short POKER[54] = {

	0x0103, 0x0104, 0x0105, 0x0106, 0x0107, 0x0108, 0x0109, 0x010A, 0x010B, 0x010C, 0x010D, 0x010E, 0x010F, 
	0x0203, 0x0204, 0x0205, 0x0206, 0x0207, 0x0208, 0x0209, 0x020A, 0x020B, 0x020C, 0x020D, 0x020E, 0x020F, 
	0x0303, 0x0304, 0x0305, 0x0306, 0x0307, 0x0308, 0x0309, 0x030A, 0x030B, 0x030C, 0x030D, 0x030E, 0x030F, 
	0x0403, 0x0404, 0x0405, 0x0406, 0x0407, 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x040D, 0x040E, 0x040F, 
	0x0501, 0x0502
};

//######################################################
poker::poker(short _c){
	card = _c;
}
poker::~poker(){}

short poker::getPokerColor(){
	return (card&0xFF00);
}
short poker::getPokerValue(){
	return (card&0x00FF);
}

short poker::getPoker(){
	return card;
}

bool poker::operator<(poker &c2){
	if( this->getPokerValue() < c2.getPokerValue() ){
		return true;
	}else{
		if( this->getPokerColor() < c2.getPokerColor() ){
			return true;
		}
	}
	return false;
}

bool poker::is(short poker){
	return (card==poker);
}


//######################################################
pokerVec::pokerVec(){
	pokers.clear();
}
/*
pokerVec::pokerVec( vector<poker> &vec){
	pokerVec();
	pokers = vec;
}
*/

pokerVec::pokerVec(short *cards, unsigned int size){
	pokerVec();
	for(unsigned int i = 0; i<size; i++){
		pokers.push_back(cards[i]);
	}
}

pokerVec::~pokerVec(){}

int pokerVec::pokersSort(){
	if( pokers.size() > 1 ){
		//std::sort(pokers.begin(), pokers.end());
	}
	return 0;
}

bool pokerVec::isInPokers(pokerVec &_p){
	_p.pokersSort();
	this->pokersSort();
	unsigned int index = 0;
	for(unsigned int i; i<_p.size(); i++){
	/*
		if( pokers[index] == _p[i] ){
			index++;
		}
		*/
	}
	if(index == pokers.size()){
		return true;
	}
	return false;
}

bool pokerVec::isBiggerThan(pokerVec &p){
	return true;
}

void pokerVec::push_back(const poker &_p){
	pokers.push_back(_p);
}

void pokerVec::push_back(short _p){
	poker p(_p);
	pokers.push_back(p);
}

POKER_TYPE pokerVec::getpokersType(){
	unsigned int len = pokers.size();
	if( len <= 4 ){
		switch( len ){
			case 1:
				return ONE_POKER;
			case 2:
				if( pokers[0].is(0x0501) && pokers[1].is(0x0502) ){
					return SMALL_BIG;
				}else if( pokers[0].getPokerValue() == pokers[1].getPokerValue() ){
					return TWO_PAIR;
				}
				break;
			case 3:
				if((pokers[0].getPokerValue() == pokers[1].getPokerValue()) 
					&& (pokers[1].getPokerValue() == pokers[2].getPokerValue())){
					return THREE_STRIP;
				}
				break;
			case 4:
				for(unsigned int i=0; i<3; i++){
					if(pokers[i].getPokerValue() != pokers[i+1].getPokerValue()){
						goto TAG_NONE;
					}
				}
				return FOUR_BOM;
		}
	}
TAG_NONE:
	return NONE;
}

int pokerVec::getPokerNum(){
	return pokers.size();
}

unsigned int pokerVec::size(){
	return pokers.size();
}

int pokerVec::clear(){
	return 0;
}

bool pokerVec::empty(){
	return true;
}

bool pokerVec::operator-(pokerVec &p){
	unsigned int size = p.size();
	printf("the - size : %d\n", size);
	return true;
}

poker pokerVec::operator[](unsigned int index){
	return pokers[index];
}

//######################################################

void shufflePoker(pokerVec &v1, pokerVec &v2, pokerVec &v3, pokerVec &bottom){

	assert( v1.empty() && v2.empty() && v3.empty() && bottom.empty() );

	short tmpPoker[54];
	srand(time(NULL));
	memcpy(tmpPoker, POKER, 54*sizeof(short));
	short tmp;
	for(int i=0; i<54; i++){
		int c = random()%54;
		tmp = tmpPoker[i];
		tmpPoker[i] = tmpPoker[c];
		tmpPoker[c] = tmp;
	}
	
	int count = 0;
	for(int i=0; i<17; i++){
		v1.push_back(tmpPoker[count++]);
		v2.push_back(tmpPoker[count++]);
		v3.push_back(tmpPoker[count++]);
	}
	for(int i=0; i<3; i++){
		bottom.push_back(tmpPoker[count++]);
	}
	assert(count==54);

	printf("i:1\t");
	for(int j=0; j<17; j++){
		printf("%d,", v1[j].getPoker());
	}
	printf("\ni:2\t");
	for(int j=0; j<17; j++){
		printf("%d,", v2[j].getPoker());
	}
	printf("\ni:3\t");
	for(int j=0; j<17; j++){
		printf("%d,", v3[j].getPoker());
	}
	printf("\nbottom\t");
	for(int j=0; j<3; j++){
		printf("%d,",bottom[j].getPoker());
	}
	printf("\n");
	return;
}
